<template>
	<div class="container add_edit">
		<el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
			<el-form-item label="菜单模块">
				<tree-select ref="tree" :data="menuOptions" :defaultProps="defaultProps"
					:checkedKeys="defaultCheckedKeys" nodeKey="_id" :defaultExpandAll="false"
					@popoverHide="popoverHide"></tree-select>
				<div class="remark_txt">非必填</div>
			</el-form-item>
			<el-form-item label="权限ID" prop="permission_id" ref="permission_id">
				<el-input v-model="form.permission_id" placeholder="请输入权限ID" :disabled="Boolean(dataId)" clearable>
				</el-input>
				<div class="remark_txt">权限唯一标识，不可修改，不允许重复</div>
			</el-form-item>
			<el-form-item label="权限名称" prop="permission_name" ref="permission_name">
				<el-input v-model="form.permission_name" placeholder="请输入权限名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="备注">
				<el-input v-model="form.comment" placeholder="请输入备注" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-button :loading="formLoading" round icon="el-icon-check" class="confirm-btn"
					@click="submitForm('ruleForm')">提交</el-button>
				<el-button round icon="el-icon-back" class="return-btn" @click="returnPage">返回</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	const __name = 'uni-id-permissions';
	const __menu = 'opendb-admin-menus';
	export default {
		data() {
			return {
				dataId: '',
				formLoading: false,
				form: {
					menu_id: '',
					permission_id: '',
					permission_name: '',
					comment: ''
				},
				rules: {
					permission_id: [{
						required: true,
						message: '权限ID不能为空',
						trigger: 'change'
					}],
					permission_name: [{
						required: true,
						message: '权限名称不能为空',
						trigger: 'change'
					}]
				},
				menuOptions: [],
				defaultProps: {
					children: 'children',
					label: 'name'
				},
				defaultCheckedKeys: []
			};
		},
		methods: {
			async loadData() {
				await this.$db[__menu]
					.noshopid()
					.noaction()
					.where({
						enable: true
					})
					.totree({
						orderby: 'sort asc',
						rows: 100,
						startWith: 'parent_id == ""'
					})
					.then(res => {
						if (res.code == 200) {
							this.menuOptions = res.datas;
						}
					});

				if (!this.dataId) {
					return;
				}

				await this.$db[__name]
					.noshopid()
					.noaction()
					.tofirst(this.dataId).then(res => {
						if (res.code == 200) {
							for (let item in this.form) {
								this.form[item] = res.datas[item];
							}

							if (this.form.menu_id) {
								this.defaultCheckedKeys = [];
								this.defaultCheckedKeys.push(this.form.menu_id);
								uni.$emit('setTreeDftVal', this.defaultCheckedKeys);
							}
						}
					});
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						this.formLoading = true;
						if (!this.dataId) {
							this.$db[__name]
								.noshopid()
								.noaction()
								.add(this.form).then(res => {
									if (res.code == 200) {
										this.getOpenerEventChannel().emit('loadData');
										this.returnPage();
									}
									this.formLoading = false;
								}).catch(() => {
									this.formLoading = false;
								})
						} else {
							this.$db[__name]
								.noshopid()
								.noaction()
								.update(this.dataId, this.form).then(res => {
									if (res.code == 200) {
										this.getOpenerEventChannel().emit('loadData');
										this.returnPage();
									}
									this.formLoading = false;
								}).catch(() => {
									this.formLoading = false;
								})
						}
					}
				});
			},
			popoverHide(checkedIds) {
				if (checkedIds) {
					this.form.menu_id = checkedIds;
				}
			},
			returnPage() {
				uni.$emit('closeTab');
				uni.redirectTo({
					url: '/pages/system_set/auth/operation_auth'
				});
			}
		},
		created() {
			this.dataId = this.$route.query.id || '';
			this.loadData();
		}
	};
</script>

<style></style>
